import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_one/class/appsetting/setting.dart';
import 'package:flutter_one/class/extension/ProgressHudExtension.dart';
import 'package:flutter_one/class/utils/Utils.dart';

enum EmptyViewType { no_message, no_record, no_network, no_order }

const List<String> _emptyViewTypeString = [
  //no_message
  "没有消息",
  //no_record
  "没有记录",
  //no_network
  "服务器开了小差~~，点我重试",
  //no_order
  "没有订单"
];

const List<String> _emptyViewImageUri = [
  //no_message
  "images/empty/empty_no_message.png",
  //no_record
  "images/empty/empty_no_record.png",
  //no_network
  "images/empty/empty_no_network.png",
  //no_order
  "images/empty/empty_no_order.png"
];

/*空视图*/
class BaseEmptyView extends StatefulWidget {
  const BaseEmptyView(
      {super.key, required this.orignalEmptyType, this.tapAction});

  //原有的类型
  final EmptyViewType orignalEmptyType;
  final tapAction;

  @override
  State<BaseEmptyView> createState() =>
      _BaseEmptyViewState(this.orignalEmptyType, this.tapAction);
}

class _BaseEmptyViewState extends State<BaseEmptyView> {
  late final EmptyViewType emptyType;
  late final tapAction;

  _BaseEmptyViewState(this.emptyType, this.tapAction);

  late var netWorkState = ConnectivityResult.none;
  late StreamSubscription<ConnectivityResult> _connectivitySubscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    //监听网络
    _connectivityInitState();
    _initConnectivity();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: _setBody,
      backgroundColor: K_APP_BACKGROUND_COLOR,
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    //移除网络监听
    _connectivitySubscription.cancel();
  }
}

/*扩展*/
extension on _BaseEmptyViewState {
  /*图片信息*/
  Widget get _setImage => Image.asset(
        netWorkState == ConnectivityResult.none
            ? _emptyViewImageUri[2]
            : _emptyViewImageUri[emptyType.index],
        height: 144,
        width: 205,
        fit: BoxFit.cover, //图像应该尽可能小，但覆盖整个渲染框
      );

  /*空视图文本*/
  Widget get _setText => Text(
        netWorkState == ConnectivityResult.none
            ? _emptyViewTypeString[2]
            : _emptyViewTypeString[emptyType.index],
        textAlign: TextAlign.center,
        style: TextStyle(
            fontSize: 15,
            fontWeight: FontWeight.bold,
            color: Utils.shareInstance.hexToInt(0x999999)),
      );

  /*空视图内容*/
  Widget get _setBody => Align(
        alignment: Alignment.center,
        child: SizedBox(
          width: 205,
          height: 165,
          child: GestureDetector(
            onTap: () {
              tapAction();
            },
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [_setImage, Expanded(child: _setText)],
            ),
          ),
        ),
      );

  //MARK: - 网络信息
  /*网络初始状态*/
  void _connectivityInitState() {
    _connectivitySubscription = Connectivity()
        .onConnectivityChanged
        .listen((ConnectivityResult result) {
      debugPrint(result.toString());

      // ignore: invalid_use_of_protected_member
      setState(() {
        netWorkState = result;
      });

      if (result == ConnectivityResult.none) {
        ProgressHudExtension.showErrorDialog("(〒︿〒)世界上最遥远的距离就是没有网~", context);
      }
    });

    _connectivitySubscription.resume();
  }

  /*网络进行监听*/
  Future<Null> _initConnectivity() async {
    try {
      await Connectivity().checkConnectivity().then((value) {
        // ignore: invalid_use_of_protected_member
        setState(() {
          netWorkState = value;
        });
      });
    } on PlatformException catch (e) {
      debugPrint(e.toString());
      ProgressHudExtension.showInfoDialog(e.toString(), context);
    }
  }
}
